package leetcode.剑指offer.前40;

import leetcode.剑指offer.pojo.TreeNode;
import org.junit.Test;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author :songyaozhang
 * @date :Created 2021/7/27 21:17
 * @description :https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof/
 */
public class 剑指_27_二叉树的镜像 {
    @Test
    public void test() {
        Solution solution = new Solution();
        TreeNode node4 = new TreeNode(4);
        TreeNode node2 = new TreeNode(2);
        TreeNode node7 = new TreeNode(7);
        TreeNode node1 = new TreeNode(1);
        TreeNode node3 = new TreeNode(3);
        TreeNode node6 = new TreeNode(6);
        TreeNode node9 = new TreeNode(9);
        node4.left = node2;
        node4.right = node7;
        node2.left = node1;
        node2.right = node3;
        node7.left = node6;
        node7.right = node9;
        TreeNode root = solution.mirrorTree(node4);
        System.out.println(bfs(root));
    }



    class Solution {
        public TreeNode mirrorTree(TreeNode root) {
            if (root == null) return null;
            TreeNode left = root.left;
            root.left = mirrorTree(root.right);
            root.right = mirrorTree(left);
            return root;
        }
    }

    private List<Integer> bfs(TreeNode node) {
        ArrayList<Integer> list = new ArrayList<>();
        if (node == null) return list;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(node);
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode cur = queue.poll();
                list.add(cur.val);
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }
            }
        }
        return list;
    }


}
